﻿using CertApply.Component;
using PluginForCertHandOut;
using Service;
using Service.Model;

namespace CertApply
{
    public partial class frmEditCertDistribute : Form
    {
        public CertHandOutConfig certHandOutConfig=new CertHandOutConfig();
        private List<ICertHandOut> certHandOut=new List<ICertHandOut>();
        public frmEditCertDistribute()
        {
            InitializeComponent();
            bindcbo_CertHandOutPlugin();
        }
        public frmEditCertDistribute(CertHandOutConfig certHandOutConfig)
        {
            InitializeComponent();
            cbo_ComputerServiceOprationPlugin.SelectedIndexChanged -= cbo_ComputerServiceOprationPlugin_SelectedIndexChanged;
            certHandOutConfig = certHandOutConfig;
            bindcbo_CertHandOutPlugin();
            txtName.Text = certHandOutConfig.Name;
            cbo_ComputerServiceOprationPlugin.SelectedValue = certHandOutConfig.PluginIdentity;
            var target = certHandOut.FirstOrDefault(s => s.Identity == certHandOutConfig.PluginIdentity);
            var allKeys = target.ConfigKey.ToList();
            foreach (var key in allKeys)
            {
                if (!certHandOutConfig.Config.Keys.Contains(key.Key))
                {
                    certHandOutConfig.Config.Add(key.Key,"");
                    certHandOutConfig.ConfigItems.Add(new Service.Model.ConfigItem()
                    {
                        DisplayName = key.DisplayName,
                        Key = key.Key,
                        IsSecret = key.IsSecret,
                    });
                }
            }
            List<FormItem> formItems = new List<FormItem>();
            foreach (var item in certHandOutConfig.Config)
            {
                var configKey = certHandOutConfig.ConfigItems.FirstOrDefault(s => s.Key == item.Key);
                formItems.Add(new FormItem()
                {
                    Key = item.Key,
                    DisplayName = configKey.DisplayName,
                    IsSecret = configKey.IsSecret,
                    Value = item.Value
                });
            }
            flowLayoutPanelcertHandOutConfig.Controls.Clear();
            flowLayoutPanelcertHandOutConfig.Controls.AddRange(formItems.ToArray());
            cbo_ComputerServiceOprationPlugin.SelectedIndexChanged += cbo_ComputerServiceOprationPlugin_SelectedIndexChanged;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtName.Text))
            {
                labMsg.Text = "请输入名称";
                labMsg.ForeColor = Color.Red;
                return;
            }
            if (cbo_ComputerServiceOprationPlugin.SelectedValue == null)
            {
                labMsg.Text = "请选择插件";
                return;
            }
            certHandOutConfig = new CertHandOutConfig();
            var selectedPlugin= certHandOut.FirstOrDefault(s => s.Identity == cbo_ComputerServiceOprationPlugin.SelectedValue.ToString());
            certHandOutConfig.Name = txtName.Text;
            certHandOutConfig.PluginIdentity = selectedPlugin.Identity;
            certHandOutConfig.ConfigItems = new List<Service.Model.ConfigItem>();
            foreach (var item in selectedPlugin.ConfigKey)
            {
                certHandOutConfig.ConfigItems.Add(new Service.Model.ConfigItem()
                {
                    Key = item.Key,
                    DisplayName = item.DisplayName,
                    IsSecret = item.IsSecret,
                });
            }
            Dictionary<string,string> config= new Dictionary<string,string>();
            foreach (FormItem control in flowLayoutPanelcertHandOutConfig.Controls)
            {
                config.Add(control.Key, control.Value);
            }
            certHandOutConfig.Config = config;
            DialogResult = DialogResult.OK;
        }
        private void bindcbo_CertHandOutPlugin()
        {
            certHandOut = PluginFactory.CreateCertHandOutInstance();
            cbo_ComputerServiceOprationPlugin.DisplayMember = "Identity";
            cbo_ComputerServiceOprationPlugin.ValueMember = "Identity";
            cbo_ComputerServiceOprationPlugin.DataSource = certHandOut;
        }
        private void frmEditCertDistribute_Load(object sender, EventArgs e)
        {
            
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbo_ComputerServiceOprationPlugin_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbo_ComputerServiceOprationPlugin.SelectedValue != null)
            {
                var selectedItem = certHandOut.FirstOrDefault(s => s.Identity == cbo_ComputerServiceOprationPlugin.SelectedValue.ToString());
                toolTip.SetToolTip(cbo_ComputerServiceOprationPlugin, selectedItem.Description);
                List<FormItem> formItems = new List<FormItem>();
                foreach (var item in selectedItem.ConfigKey)
                {
                    formItems.Add(new FormItem()
                    {
                        Key = item.Key,
                        DisplayName = item.DisplayName,
                        IsSecret = item.IsSecret
                    });
                }
                flowLayoutPanelcertHandOutConfig.Controls.Clear();
                flowLayoutPanelcertHandOutConfig.Controls.AddRange(formItems.ToArray());
            }
        }
    }
}
